package com.example.reflect.demo07.annotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo {
    public static void main(String[] args) {
        //获取类对象
        Class<Person> personClass = Person.class;

        //判断@Report是否存在于Person类
        boolean classAnnotationPresent = personClass.isAnnotationPresent(Report.class);
        if (classAnnotationPresent){
            System.out.println("@Report存在于Person类上");
        }
        else{
            System.out.println("@Report不存在于Person类上");
        }


        Field[] fields = personClass.getFields();
        for (Field field:fields){
            boolean fieldAnnotationPresent = field.isAnnotationPresent(Report.class);
            if (fieldAnnotationPresent){
                System.out.println("@Report存在于Personzi字段"+field.getName()+"上");
            }
            else {
                System.out.println("@Report不存在于Personzi字段上");
            }
        }

        Method[] methods=personClass.getMethods();
        for (Method method:methods){
            boolean methodAnnotationPresent = method.isAnnotationPresent(Report.class);
            if (methodAnnotationPresent){
                System.out.println("@Report存在于Personzi方法"+method.getName()+"上");
            }
            else {
                System.out.println("@Report不存在于Personz方法上");
            }
        }

        Constructor[] constructors=personClass.getConstructors();
        for (Constructor constructor:constructors){
            boolean constructorAnnotationPresent = constructor.isAnnotationPresent(Report.class);
            if (constructorAnnotationPresent){
                System.out.println("@Report存在于Personzi构造方法"+constructor.getName()+"上");
            }
            else {
                System.out.println("@Report不存在于Personz构造方法上");
            }
        }

        Class cls1 = Person.class;
        if (cls1.isAnnotationPresent(Report.class)) {
            Report report = (Report) cls1.getAnnotation(Report.class);
            int type= report.type();
            String level= report.level();
            String value= report.value();
            System.out.println("第一种方式获取Report,type-level-value="+type+"-"+level+"-"+value);
        }
        else {
            System.out.println("第一种方式没有获取到Report");
        }

        Class cls2 = Person.class;
        Report report =  (Report)cls2.getAnnotation(Report.class);
        if (report != null) {
            int type= report.type();
            String level= report.level();
            String value= report.value();
            System.out.println("第二种方式获取Report,type-level-value="+type+"-"+level+"-"+value);
        }
        else {
            System.out.println("第二种方式没有获取到Report");
        }
    }
}
